package com.vn.ducktohome.screen;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.vn.ducktohome.constant.Constant;
import com.vn.ducktohome.enums.ScreenEnum;
import com.vn.ducktohome.gamelogic.GameControl;
import com.vn.ducktohome.screen.play.LevelHelper;
import com.vn.ducktohome.screen.selectlevel.LevelBox;

public class SelectLevelScreen extends BaseScreen{

	private Image selectLevel;
	private TextButton mainMenu, tbResetLevel;
	private Label lLives1, lLives2;
	//Dùng để kiểm tra xem ấn nút reset bao nhiêu lần.
	private boolean bReset;
	
	@Override
	public void show() {
		super.show();
		selectLevel = new Image(GameControl.getAtlas().findRegion("select_level/select_level"));
		stage.addActor(selectLevel);
		bReset = false;
		initButton();
		readLevel();
		initLevelBox();
		initLives();
	}
	
	private void initLives() {
		lLives1 = new Label("Lives : ", GameControl.getMySkin());
		lLives1.setColor(Color.RED);
		lLives1.setX(50);
		lLives1.setY(445);
		stage.addActor(lLives1);
		
		lLives2 = new Label(String.valueOf(Constant.LIVES), GameControl.getMySkin());
		lLives2.setColor(Color.RED);
		lLives2.setX(120);
		lLives2.setY(445);
		stage.addActor(lLives2);
	}

	private void readLevel() {
		LevelHelper.readLevel();
	}

	private void initButton(){
		mainMenu = new TextButton("Main menu", GameControl.getMySkin(), "button-red");
		mainMenu.setX(430);
		mainMenu.setY(100);
		mainMenu.setHeight(50);
		mainMenu.setWidth(150);
		stage.addActor(mainMenu);
		mainMenu.addListener(new ClickListener(){

			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				super.touchUp(event, x, y, pointer, button);
				GameControl.getScreenManager().createScreen(ScreenEnum.MENU_SCREEN);
				GameControl.getScreenManager().getScreen().show();
			}
		});
		
		tbResetLevel = new TextButton("Reset level", GameControl.getMySkin(), "button-red");
		tbResetLevel.setX(250);
		tbResetLevel.setY(100);
		tbResetLevel.setHeight(50);
		tbResetLevel.setWidth(150);
		stage.addActor(tbResetLevel);
		tbResetLevel.addListener(new ClickListener() {
			@Override
			public void touchUp(InputEvent event, float x, float y,
					int pointer, int button) {
				super.touchUp(event, x, y, pointer, button);
				LevelHelper.resetLevel();
				initLevelBox();
				lLives2.setText(String.valueOf(Constant.LIVES));
				bReset = true;
			}
		});
	}
	
	private void initLevelBox(){
		if (bReset == false){
			for (Actor a : stage.getRoot().getChildren())
				if (a.getName() == "gLevelBox")
					a.setVisible(false);
		
			int maxCol = 8;
			int row = 1;
			for (int i = 1; i <= 24; i++) {
				if (i > 8)
					row = 2;
				if (i > 16)
					row = 3;
				boolean bOn = false;
				if (i <= Constant.LEVEL)
					bOn = true;
				LevelBox lB = new LevelBox(bOn, i);
				lB.setX(50 + (i-1) % maxCol * 90);
				lB.setY(450 - row * 80);
				Group gLevelBox = new Group();
				gLevelBox.setName("gLevelBox");
				gLevelBox.addActor(lB);
				stage.addActor(gLevelBox);
			}
		}
	}

	@Override
	public void render(float arg0) {
		super.render(arg0);
	}
}